package com.hz.house.utils.SMS;

import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import javax.annotation.Resource;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;

/**
 * 短信发送工具
 */
@Component
public class SmsSendUtils {
    @Resource
    private RestTemplate restTemplate;
    final String SMS_BASE_URL = "http://101.201.140.197:8080/sms.aspx?" +
            "action={action}" +
            "&userid={userid}" +
            "&account={account}" +
            "&password={password}" +
            "&mobile={mobile}" +
            "&content={content}" +
            "&sendTime={sendTime}" +
            "&extno={extno}"; //http://101.207.117.221:7777

    public Map<String, Object> send(String cellphone, String code) {
        Map<String, Object> params = new HashMap<>(8);
        params.put("action", "send");
        params.put("userid", "290");
        params.put("password", "123456");
        params.put("account", "hztx");
        params.put("mobile", cellphone);
        params.put("sendTime", "");
        params.put("extno", "");
        params.put("content", "【买房好帮手】尊敬的用户，您的验证码是:" + code + "，敬请使用。");
        String res = restTemplate.getForObject(SMS_BASE_URL, String.class, params);
        return getXmlBodyContext(res);
    }

    public Map<String, Object> sendSmsMessage(String cellphone, String data) {
        Map<String, Object> params = new HashMap<>(8);
        params.put("action", "send");
        params.put("userid", "290");
        params.put("password", "123456");
        params.put("account", "hztx");
        params.put("mobile", cellphone);
        params.put("sendTime", "");
        params.put("extno", "");
        params.put("content", data);
        String res = restTemplate.getForObject(SMS_BASE_URL, String.class, params);
        return getXmlBodyContext(res);
    }


    public static Document parseXmlString(String xmlStr) {
        try {
            InputSource is = new InputSource(new StringReader(xmlStr));
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(is);
            return doc;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Map<String, Object> getXmlBodyContext(String bodyXml) {
        Map<String, Object> dataMap = new HashMap<>(6);
        Document doc = parseXmlString(bodyXml);
        if (null != doc) {
            NodeList rootNode = doc.getElementsByTagName("returnsms");
            if (rootNode != null) {

                Node root = rootNode.item(0);
                NodeList nodes = root.getChildNodes();
                for (int i = 0; i < nodes.getLength(); i++) {
                    Node node = nodes.item(i);
                    dataMap.put(node.getNodeName(), node.getTextContent());
                }
            }
        }
        return dataMap;
    }

}
